import api from './api.js'
import {
	HTTP_REQUEST_URL
} from '@/config.js'

// 登录状态
const loginState = async function(type) {
	let uid = uni.getStorageSync('userInfo').id;
	let isLogin = '';

	if (type && type == 'reset') {
		if (!uid) {
			uni.showToast({
				icon: 'loading',
				title: '请先登录...',
				mask: true,
				duration: 1000
			})
			uni.removeStorageSync('userInfo');
			uni.removeStorageSync('cookiekey');
			setTimeout(() => {
				uni.navigateTo({
					url: '/pages/login/login?backType=prev'
				})
			}, 1000)
		} else {
			let res = await api.user_api.checkLogin();
			console.log('登录状态', res)
			if (res.data.ret != 0) {
				uni.showToast({
					icon: 'loading',
					title: '登录已超时...',
					mask: true,
					duration: 1000
				})
				uni.removeStorageSync('userInfo');
				uni.removeStorageSync('cookiekey');

				setTimeout(() => {
					uni.navigateTo({
						url: '/pages/login/login?backType=prev'
					})
				}, 1000)
				isLogin = false;
			} else {
				isLogin = true;
			}
		}

	} else if (type && type == 'check') {
		if (!uid) {
			uni.showToast({
				icon: 'loading',
				title: '请先登录...',
				mask: true,
				duration: 1000
			})
			uni.removeStorageSync('userInfo');
			uni.removeStorageSync('cookiekey');
			setTimeout(() => {
				uni.navigateTo({
					url: '/pages/login/login?backType=prev'
				})
			}, 1000)
			isLogin = false;
		} else {
			let res = await api.user_api.checkLogin();
			console.log('登录状态', res)
			if (res.data.ret != 0) {
				uni.showToast({
					icon: 'loading',
					title: '登录已超时...',
					mask: true,
					duration: 1000
				})
				uni.removeStorageSync('userInfo');
				uni.removeStorageSync('cookiekey');

				setTimeout(() => {
					uni.navigateTo({
						url: '/pages/login/login?backType=prev'
					})
				}, 1000)
				isLogin = false;
			} else {
				isLogin = true;
			}
		}
	}

	return isLogin
}

// 登录 
const login = (data) => { 
	let callback = data.success;
	let code = '';
	let _this = this;
	
	uni.getUserProfile({
		desc: '用户名',
		success: async function(res) {
			console.log('weixing',res)
			uni.login({
				provider: "weixin",
				async success(res_code) { 
					console.log(res_code)
					uni.showLoading({
						title: "授权登录中...",
						mask: true
					})
					
					uni.request({
						url: HTTP_REQUEST_URL + '/user/hhyfksspwx-login',
						data: {
							code: res_code.code,
							realname: res.userInfo.nickName 
						},
						method: 'post',
						header: {
							"Content-Type": "application/x-www-form-urlencoded",
							'Cookie': []
						},
						dataType: 'json',
						async success(p) { 
							console.log('ppppppppppp',p.header['Set-Cookie'])
							// console.log('Set-Cookie',p.header['Set-Cookie'],p.header['Set-Cookie'].split(',')[0].split(';')[0]+';'+p.header['Set-Cookie'].split(',')[1].split(';')[0])
							// uni.setStorage({
							// 	key:'cookiekey',
							// 	data:p.header['Set-Cookie']
							// });	
							uni.setStorage({
								key:'cookiekey', 
								data: p.header['Set-Cookie'].split(',')[0].split(';')[0]+';'+p.header['Set-Cookie'].split(',')[1].split(';')[0]
							});	 
							if (p.data.ret == 0) { 
								let save_cookie = await api.user_api.saveCookie();
								console.log('save_cookie',save_cookie)
								uni.switchTab({
									url: '/pages/index/index'
								})
								uni.setStorage({
									key: 'userInfo',
									data: p.data.data
								})
							} else if (p.data.ret == -5) {
								uni.navigateTo({
									url: '/pages/bindPhone/bindPhone?type=bind&openid=' +	p.data.message
								})
							} 
							 
						}  
					});
					
					
					 
					
					 
				}
			})
	
		}
	})
	
		
	// uni.getUserProfile({
	// 	desc: '用户名',
	// 	success: async function(res) {
	// 		console.log(res)
	// 		uni.login({
	// 			provider: "weixin",
	// 			async success(res_code) { 
	// 				// 获取cookie
	// 				uni.request({
	// 					url: HTTP_REQUEST_URL + '/user/get-cookie',
	// 					data: {},
	// 					method: 'post',
	// 					header: {
	// 						"Content-Type": "application/x-www-form-urlencoded",
	// 						'Cookie': []
	// 					},
	// 					dataType: 'json',
	// 					async success(p) {
	// 						console.log('getcookie',p.header['Set-Cookie'])
	// 						uni.setStorage({
	// 							key:'cookiekey',
	// 							data:p.header['Set-Cookie']
	// 						});
							
	// 						uni.showLoading({
	// 							title: "授权登录中...",
	// 							mask: true
	// 						})
							
	// 						let login_res = await api.user_api.wxLogin({
	// 							code: res_code.code,
	// 							realname: res.userInfo.nickName
	// 						})
							
	// 						console.log('login_res',login_res)
	// 						if (login_res.data.ret == 0) {	
	// 							console.log('getcookie1',login_res.header['Set-Cookie'])
	// 							let save_cookie = await api.user_api.saveCookie();
	// 							uni.switchTab({
	// 								url: '/pages/index/index'
	// 							})
	// 							uni.setStorage({
	// 								key: 'userInfo',
	// 								data: login_res.data.data
	// 							})
	// 						} else if (login_res.data.ret == -5) {
	// 							uni.navigateTo({
	// 								url: '/pages/bindPhone/bindPhone?openid=' +	login_res.data.message
	// 							})
	// 						} 
							
							
	// 					}  
	// 				});
					
					
					 
					
					 
	// 			}
	// 		})
	
	// 	}
	// })

	

	console.log(code)


}


export {
	loginState,
	login
}
